package data.account.attendee;

import java.util.Scanner;

import sql.SQLInterface;

import data.CMSRes;
import data.account.Account;
import data.account.attendee.lodging.LodgingData;
import data.account.attendee.travel.TravelData;
import data.event.Event;
import applet.attendee.CMSAttendeeView;
import applet.view.CMSView;

public class AttendeeAccount implements Account, Comparable<AttendeeAccount>
{
	private String userID;
	public String userID() { return userID; }
	
	private String password;
	public String password() { return password; }
	
	private String firstName;
	public String firstName() { return firstName; }
	public void setFirstName(String firstName) { this.firstName = firstName; }
	
	private String lastName;
	public String lastName() { return lastName; }
	public void setLastName(String lastName) { this.lastName = lastName; }
	
	public String fullName() { return firstName + " " + lastName; }
	
	private String org;
	public String org() { return org; }
	public void setOrg(String organization) { this.org = organization; }
	
	private boolean checkedIn;
	public boolean checkedIn() { return checkedIn; }
	public boolean canCheckIn()
	{
		int numEvents = 0;
		for(Event event : CMSRes.conference().events())
			if(event.attendees().contains(userID))
				numEvents++;
		return numEvents > 0 && firstName.length() > 0 && lastName.length() > 0;
	}
	public void checkIn() { checkedIn = true; saveData(); }
	
	private TravelData travel;
	public TravelData travel() { return travel; }
	public void setTravel(TravelData travel) { this.travel = travel; }

	private LodgingData lodging;
	public LodgingData lodging() { return lodging; }
	public void setLodging(LodgingData lodging) { this.lodging = lodging; }

	@Override public CMSView createView() { return new CMSAttendeeView(this); }
	
	public AttendeeAccount(String userID, String password)
	{
		this.userID = userID;
		this.password = password;
		this.firstName = "";
		this.lastName = "";
		org = "";
		checkedIn = false;
		lodging = null;
		travel = null;
	}
	
	public AttendeeAccount(String userID, String password, String data)
	{
		this.userID = userID;
		this.password = password;
		setFromData(data);
	}
	
	public void setFromData(String data)
	{
		System.out.println("Loading attendee data from: " + data);
		Scanner dataScanner = new Scanner(data);
		dataScanner.useDelimiter("#");
		
		firstName = dataScanner.next();
		if(firstName.equals("_"))
			firstName = "";
		System.out.println("First Name: ["+ firstName + "]");
		
		lastName = dataScanner.next();
		if(lastName.equals("_"))
			lastName = "";
		System.out.println("Last Name: ["+ lastName + "]");
		
		org = dataScanner.next();
		if(org.equals("_"))
			org = "";
		System.out.println("Organization: ["+ org + "]");
		
		String cI = dataScanner.next();
		System.out.println("Checked in:" + cI);
		checkedIn = Boolean.parseBoolean(cI);
		
		String travel = dataScanner.next();
		System.out.println("Loading travel: " + travel);
		if(!travel.equals("null"))
			this.travel = new TravelData(travel);

		String lodging = dataScanner.next();
		System.out.println("Loading lodging: " + lodging);
		if(!lodging.equals("null"))
			this.lodging = new LodgingData(lodging);
	}
	
	public String toData()
	{
		return 	(firstName.length() == 0 ? "_" : firstName) + "#" + 
				(lastName.length() == 0 ? "_" : lastName) + "#" + 
				(org.length() == 0 ? "_" : org) + "#" + 
				checkedIn + "#" +
				((travel == null) ? "null" : travel.toData()) + "#" +
				((lodging == null) ? "null" : lodging.toData());
	}
	
	public void saveData() { SQLInterface.saveAttendee(this); }
	public void checkOut() { SQLInterface.deleteAccount(this); }
	
	@Override public int compareTo(AttendeeAccount a)
	{
		int c = lastName.compareTo(a.lastName);
		if(c == 0)
		{
			c = firstName.compareTo(a.firstName);
			if(c == 0)
				c = org.compareTo(a.org);
		}
		return c;
	}
}